package com.csst.business.dao.mysql.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.csst.business.dao.IOnlineDao;
import com.csst.business.model.BOnline;
import com.csst.business.model.TCompany;
import com.csst.business.model.TTrUser;
import com.csst.business.model.TUserMenu;

public class OnlineDaoImpl extends BaseDaoImpl implements IOnlineDao {

	@Override
	public void save(Object o) {
		
	}

	@Override
	public TTrUser findUserById(String id) {
		return null;
	}

	@Override
	public BOnline saveOnline(BOnline online) {
		Session session=getSessionFactory().getCurrentSession();
		session.save(online);
		session.flush();
		session.clear();
		return online;
	}

	@Override
	public List<BOnline> getOnlineList(String userId) {
		Session session=getSessionFactory().getCurrentSession();
		StringBuffer sb=new StringBuffer();
		sb.append(" from BOnline t where 1=1 and t.OpenID='"+userId+"'and t.isdel='1' order by t.OPretime");
		List<BOnline> list=session.createQuery(sb.toString()).list();
		session.flush();
		session.clear();
		return list;
	}

	@Override
	public int getOrderTotle() {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from BOnline t where 1=1 and  t.isdel='1' order by t.OPretime desc";
		Query q=session.createQuery(sql);
		List list = q.list();
		session.flush();
		session.clear();
		return list.size();
	}

	@Override
	public List<BOnline> getAllOnlineList(int st,int ed) {
		Session session=getSessionFactory().getCurrentSession();
		StringBuffer sb=new StringBuffer();
		sb.append(" from BOnline t where 1=1 and t.isdel='1' order by t.OPretime");
		Query q = session.createQuery(sb.toString());
		q.setFirstResult(st-1);
		q.setMaxResults(ed);
		List<BOnline> list=q.list();
		session.flush();
		session.clear();
		return list;
	}

	@Override
	public boolean deleteOrderById(String id) {
		Session session=getSessionFactory().getCurrentSession();
		String hql=" delete from BOnline t   where t.id='"+id+"'";
		session.createQuery(hql).executeUpdate();
		session.flush();
		session.clear();
		return true;
	}

	@Override
	public List<BOnline> getAllOnlineList(String openID, int startNum,
			int endNum) {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from BOnline t where 1=1 and  t.isdel='1' and t.OpenID='"+openID+"' order by t.OPretime desc";
		Query q = session.createQuery(sql.toString());
		q.setFirstResult(startNum-1);
		q.setMaxResults(endNum);
		List<BOnline> list=q.list();
		session.flush();
		session.clear();
		return list;
	}

	@Override
	public int getOrderTotle(String openID) {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from BOnline t where 1=1 and  t.isdel='1' and t.OpenID='"+openID+"' order by t.OPretime desc";
		Query q=session.createQuery(sql);
		List list = q.list();
		session.flush();
		session.clear();
		return list.size();
	}

}
